package com.article.realm;

import com.article.model.AdminInfo;
import com.article.service.AdminInfoService;
import com.article.service.RoleInfoService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

public class MyRealm extends AuthorizingRealm {
    @Resource
    private AdminInfoService adminInfoService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        // System.out.println("dddd");
        // String userName = (String) principals.getPrimaryPrincipal();
        // SimpleAuthorizationInfo authorizationInfo = new
        // SimpleAuthorizationInfo();
        // Set<String> roles = adminService.getRoles(userName);
        // System.out.println("dddd" + roles.size());
        // Set<String> permissions = adminService.getPermissions(userName);
        // System.out.println("ddddfff" + permissions.size());
        // authorizationInfo.setRoles(roles);
        // authorizationInfo.setStringPermissions(permissions);
        // return authorizationInfo;
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
            throws AuthenticationException {
        // System.out.println("d");
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
        AdminInfo admin = null;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("account", token.getUsername());
        admin = adminInfoService.getByMap(map);
        if (admin != null) {
            return new SimpleAuthenticationInfo(admin.getAccount(), admin.getPassword(), getName());
        } else {
            throw new AuthenticationException();
        }
    }
}
